#******************************************************************
#
# Copyright 2015 Samsung Electronics All Rights Reserved.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
Import('libocsrm_env')
import os
from distutils.dir_util import copy_tree

tools_env = libocsrm_env.Clone()
src_dir = tools_env.get('SRC_DIR')
target_os = tools_env.get('TARGET_OS')

if ((target_os == 'windows') and (tools_env.get('MSVC_UWP_APP') == '1') and \
    (not tools_env.GetOption('clean'))):
    # If you are building for UWP, then you can't build and run a regular desktop program/exe.
    # Make sure that the win32 version is built and copy it to the uwp build output directory.
    tool_path = os.path.join(tools_env.get('BUILD_DIR'), 'resource/csdk/security/tool')
    tool_path_win32 = tool_path.replace('/uwp/', '/win32/')
    json2cbor_win32_file = os.path.join(tool_path_win32, 'json2cbor' + tools_env.get('PROGSUFFIX'))
    if os.path.exists(json2cbor_win32_file):
        # Copy the win32 version to use here
        copy_tree(tool_path_win32, tool_path)
        print('Copied the win32 security tools to %s' % tool_path)
        Return()
    else:
        # win32 version doesn't exist, output message
        err_msg = 'Error: Please build the win32 version of the security tool ' + \
                  '(resource/csdk/security/tool) and then run the UWP build again'
        print(err_msg)
        Exit(1)

######################################################################
# Build flags
######################################################################
with_upstream_libcoap = tools_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
    tools_env.AppendUnique(CPPPATH=['#/extlibs/libcoap/libcoap/include'])
else:
    tools_env.AppendUnique(CPPPATH=['#/resource/csdk/connectivity/lib/libcoap-4.1.1/include'])

tools_env.PrependUnique(CPPPATH=[
    '#/extlibs/cjson',
    '#/extlibs/tinycbor/tinycbor/src',
    '#/resource/csdk/include',
    '#/resource/csdk/stack/include',
    '#/resource/csdk/stack/include/internal',
    '#/resource/csdk/logger/include',
    '#/resource/oc_logger/include',
    '#/resource/csdk/connectivity/api',
    '#/resource/csdk/connectivity/inc/pkix',
    '#/resource/csdk/security/provisioning/include',
    '#/resource/csdk/security/provisioning/include/internal'
])
tools_env.AppendUnique(CXXFLAGS=['-O2', '-g', '-Wall', '-Wextra', '-std=c++0x'])

if target_os in ['windows', 'msys_nt']:
    # Macro needed for Windows builds to avoid __declspec(dllexport) and
    # __declspec(dllimport) for cJSON APIs.
    tools_env.AppendUnique(CPPDEFINES=['CJSON_HIDE_SYMBOLS'])

if target_os not in ['darwin', 'msys_nt', 'windows']:
    tools_env.PrependUnique(LIBS=[
        'octbstack', 'ocsrm', 'm', 'mbedtls', 'mbedx509', 'mbedcrypto'
    ])
else:
    if target_os in ['darwin']:
        tools_env.AppendUnique(LIBS=['octbstack', 'connectivity_abstraction'])
    else:
        # This IoTivity-internal app calls non-Public APIs, so link with
        # internal LIBS
        tools_env.AppendUnique(LIBS=[
            'octbstack_internal',
            'connectivity_abstraction_internal'
        ])
        if target_os in ['windows']:
            tools_env.AppendUnique(LIBS=['sqlite3'])
    tools_env.AppendUnique(LIBS=[
        'coap',
        'mbedtls',
        'mbedx509',
        'mbedcrypto',
        'ocsrm',
        'routingmanager'
    ])
tools_env.PrependUnique(LIBS=['cjson'])

if tools_env.get('MULTIPLE_OWNER') == '1':
    tools_env.AppendUnique(CPPDEFINES=['MULTIPLE_OWNER'])
######################################################################
# Source files and Targets
######################################################################
json2cbor_src = ['json2cbor.c']
json2cbor = tools_env.Program('json2cbor', json2cbor_src)

SVRDBEDITOR_DIR = 'svrdbeditor_src/'
svrdbeditor_src = [
    SVRDBEDITOR_DIR + 'svrdbeditorcommon.c',
    SVRDBEDITOR_DIR + 'svrdbeditoracl.c',
    SVRDBEDITOR_DIR + 'svrdbeditorcred.c',
    SVRDBEDITOR_DIR + 'svrdbeditordoxm.c',
    SVRDBEDITOR_DIR + 'svrdbeditorpstat.c',
    SVRDBEDITOR_DIR + 'svrdbeditor.c'
]
svrdbeditor = tools_env.Program("svrdbeditor", svrdbeditor_src)
securitytools = [json2cbor, svrdbeditor]
Alias("securitytools", securitytools)
tools_env.AppendTarget('securitytools')
tools_env.UserInstallTargetExtra(securitytools)
